package top.maof.mfcoin.tx;

import lombok.Data;
import top.maof.mfcoin.block.BlockBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 交易池
 * 未打包的交易
 * 单例模式
 *
 * @author 毛逢
 * @date 2021/11/11 10:07
 */
@Data
public class TransactionPool {
    private static TransactionPool instance = new TransactionPool();

    // merkleTree的最大交易数量
    private static final int MAX_TX_NUM = 100;

    /**
     * key: tx的hash
     * value：未放入区块的Transaction
     */
    private Map<String, Transaction> transactionMap;


    private TransactionPool() {
    }


    public static TransactionPool getInstance() {
        return instance;
    }

    public static TransactionPool getInstance(int capacity) {
        instance.setTransactionMap(new HashMap<>(capacity));
        return instance;
    }

    /**
     * 从交易池中打包一个合法的merkleTree
     *
     * @author 毛逢
     * @date 2021/11/11 16:38
     */
    public BlockBody getMerkleTree(int txNum) {
        txNum = txNum > MAX_TX_NUM ? MAX_TX_NUM : txNum;
        Set<Map.Entry<String, Transaction>> entries = transactionMap.entrySet();

        BlockBody blockBody = new BlockBody();
        return null;
    }

}
